﻿using System;
using System.Collections.Generic;
using System.Data;
using Dapper;
using EP.Base.BaseService.Manager.Config;
using EP.Base.BaseService.Manager.Dal.Helper;
using EP.Base.BaseService.Manager.Interface.Data;
using EP.Base.BaseService.Manager.Model.Entity;

namespace EP.Base.BaseService.Manager.Dal
{
    public class IndexBackupLogs : IIndexBackupLogs
    {
        public bool UpdateStateById(long id, short state)
        {
            string sql = "UPDATE [IndexBackupLog] SET State=@State  WHERE Id=@Id";
            var par = new DynamicParameters();
            par.Add("@Id", id, DbType.Int32);
            par.Add("@State", state, DbType.Int16);
            return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par) > 0;
        }

        public bool UpdateStateAndCheckInfo(long id, short state, DateTime lastCheckTime, string esHandleState)
        {
            string sql = "UPDATE [IndexBackupLog] SET State=@State,EsHandelState=@EsHandelState,LastCheckTime=@LastCheckTime  WHERE Id=@Id";
            var par = new DynamicParameters();
            par.Add("@LastCheckTime", lastCheckTime, DbType.DateTime);
            par.Add("@Id", id, DbType.Int32);
            par.Add("@State", state, DbType.Int16);
            par.Add("@EsHandelState", esHandleState, DbType.AnsiString);
            return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par) > 0;
        }

        public bool UpdateStateAndDeleteTimeById(long id, short state, DateTime deleteTime)
        {
            string sql = "UPDATE [IndexBackupLog] SET State=@State,DeleteTime=@DeleteTime  WHERE Id=@Id";
            var par = new DynamicParameters();
            par.Add("@DeleteTime", deleteTime, DbType.DateTime);
            par.Add("@Id", id, DbType.Int32);
            par.Add("@State", state, DbType.Int16);
            return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par) > 0;
        }

        /// <summary>
        /// 添加IndexBackupLog
        /// </summary>
        /// <param name="indexbackuplog"></param>
        /// <returns></returns>
        public long Add(IndexBackupLog indexbackuplog)
        {
            string sql = @"INSERT INTO [IndexBackupLog]
			([IndexConfigId],[SnapshotName],[State],[CreateTime],[Repository])
			VALUES
			(@IndexConfigId,@SnapshotName,@State,@CreateTime,@Repository) 
			SELECT SCOPE_IDENTITY()
			";
            var par = new DynamicParameters();
            par.Add("@IndexConfigId", indexbackuplog.IndexConfigId, DbType.Int32);
            par.Add("@SnapshotName", indexbackuplog.SnapshotName, DbType.AnsiString);
            par.Add("@State", indexbackuplog.State, DbType.Int16);
            par.Add("@CreateTime", indexbackuplog.CreateTime, DbType.DateTime);
            par.Add("@Repository", indexbackuplog.Repository, DbType.AnsiString);
            return DapWrapper.InnerQueryScalarSql<long>(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
        }

        public List<IndexBackupLog> GetIndexBackupLogsByIndexConfidId(int indexConfigId, short state)
        {
            string sql = "select [Id],[IndexConfigId],[SnapshotName],[State],[CreateTime],[DeleteTime],[Repository],[EsHandelState],[LastCheckTime] FROM [IndexBackupLog] WHERE IndexConfigId=@IndexConfigId AND State=@State";
            var par = new DynamicParameters();
            par.Add("@IndexConfigId", indexConfigId, DbType.Int64);
            par.Add("@State", state, DbType.Int16);
            return DapWrapper.InnerQuerySql<IndexBackupLog>(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
        }
    }
}
